package com.hyperion.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyperion.blog.utils.UserThreadLocal;
import com.hyperion.blog.dao.mapper.ArticleMapper;
import com.hyperion.blog.dao.mapper.CommentsMapper;
import com.hyperion.blog.dao.pojo.Article;
import com.hyperion.blog.dao.pojo.Comment;
import com.hyperion.blog.dao.pojo.SysUser;
import com.hyperion.blog.service.CommentsService;
import com.hyperion.blog.service.SysUserService;
import com.hyperion.blog.vo.CommentVo;
import com.hyperion.blog.vo.UserVo;
import com.hyperion.blog.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Hyperion
 */
@Service
public class CommentsServiceImpl implements CommentsService {

    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private ArticleMapper articleMapper;

    private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 返回文章的评论
     *
     * @param articleId 文章 id
     * @return 该文章下的所有评论
     */
    @Override
    public List<CommentVo> commentByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getArticleId, articleId);
        queryWrapper.eq(Comment::getLevel, 1);
        queryWrapper.orderByDesc(Comment::getCreateDate);
        List<Comment> comments = commentsMapper.selectList(queryWrapper);
        if (comments == null) {
            comments = Collections.emptyList();
        }
        return copyList(comments);
    }

    /**
     * 文章评论
     *
     * @param commentParam 文章和评论信息
     */
    @Override
    public void createComment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setCreateDate(System.currentTimeMillis());
        comment.setArticleId(commentParam.getArticleId());
        comment.setContent(commentParam.getContent());
        comment.setAuthorId(sysUser.getId());
        Long parent = commentParam.getParent();
        if (parent == null || parent == 0) {
            comment.setLevel(1);
        } else {
            comment.setLevel(2);
        }
        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        if(toUserId == null || toUserId == 0){
            toUserId = commentsMapper.selectById(parent).getAuthorId();
        }
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentsMapper.insert(comment);
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getId, commentParam.getArticleId());
        Article article = articleMapper.selectOne(queryWrapper);
        article.setCommentCounts(article.getCommentCounts() + 1);
        articleMapper.updateById(article);
    }

    List<CommentVo> copyList(List<Comment> comments) {
        List<CommentVo> commentVoList = new ArrayList<>();
        for (Comment comment : comments) {
            commentVoList.add(copy(comment));
        }
        return commentVoList;
    }

    CommentVo copy(Comment comment) {
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(String.valueOf(comment.getId()));
        commentVo.setId(String.valueOf(comment.getId()));
        commentVo.setCreateDate(dateFormat.format(new Date(comment.getCreateDate())));
        Long authorId = comment.getAuthorId();
        UserVo authorUserVo = sysUserService.findUserVoById(authorId);
        commentVo.setAuthor(authorUserVo);
        Integer level = comment.getLevel();
        if (1 == level) {
            LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Comment::getParentId, comment.getId());
            queryWrapper.eq(Comment::getLevel, 2);
            List<Comment> commentList = commentsMapper.selectList(queryWrapper);
            commentVo.setChildrens(copyList(commentList));
        }
        if (level > 1) {
            Long toUid = comment.getToUid();
            UserVo toUser = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUser);
        }
        return commentVo;
    }

}
